package com.easylinkin.linkappapi.calculaterule.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.annotation.CommonOperateLogAnnotate;
import com.easylinkin.linkappapi.calculaterule.entity.CalculateRule;
import com.easylinkin.linkappapi.calculaterule.service.CalculateRuleService;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.operatelog.constant.LogConstant.LogModule;
import java.util.List;
import javax.annotation.Resource;
import javax.validation.Valid;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

/**
 * <p>
 * 计算规则 前端控制器
 * </p>
 *
 * @author TongJie
 * @since 2020-06-17
 */
@RestController
@RequestMapping("/calculaterule/calculateRule")
public class CalculateRuleController {


    @Resource
    private CalculateRuleService service;


    @PostMapping("add")
    @CommonOperateLogAnnotate(module = LogModule.CALCULATE_RULE, desc = "新增计算规则")
    public RestMessage add(@RequestBody @Valid CalculateRule calculateRule) {
        return RestBuilders.successBuilder(service.add(calculateRule)).build();
    }


    @PostMapping("get")
    public RestMessage getCalculateRules(@RequestBody CalculateRule calculateRule) {
        Assert.notNull(calculateRule, "calculateRule 不能为空");
        return RestBuilders.successBuilder().data(service.getCalculateRules(calculateRule)).build();
    }

    @PostMapping("getPage")
    public RestMessage getCalculateRulesPage(@RequestBody RequestModel<CalculateRule> requestModel) {
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<CalculateRule> record = service.getCalculateRulesPage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder().data(record).build();
    }

    @PostMapping("getById")
    public RestMessage getById(@RequestParam("id") String id) {
        Assert.hasLength(id, "id 不能为空");
        return RestBuilders.successBuilder().data(service.getCalculateRule(id)).build();
    }

    @PostMapping("update")
    @CommonOperateLogAnnotate(module = LogModule.CALCULATE_RULE, desc = "修改计算规则")
    public RestMessage update(@RequestBody CalculateRule calculateRule) {
        return RestBuilders.successBuilder(service.updateCalculateRule(calculateRule)).build();
    }


    @PostMapping("deleteBatch")
    @CommonOperateLogAnnotate(module = LogModule.CALCULATE_RULE, desc = "批量删除计算规则")
    public RestMessage deleteBatch(@RequestBody List<CalculateRule> calculateRuleList) {
        Assert.notEmpty(calculateRuleList, "参数为空");
        return RestBuilders.successBuilder(service.deleteBatch(calculateRuleList)).build();
    }


}

